package polep.wespace.utils;

import java.io.IOException;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.codehaus.jackson.JsonGenerationException;
import org.codehaus.jackson.JsonParseException;
import org.codehaus.jackson.map.JsonMappingException;
import org.codehaus.jackson.map.ObjectMapper;

public class Utils {
	private static Log log = LogFactory.getLog(Utils.class);
	
	/**
	 * 把对象转换为map 对象需要有get方法
	 * @param obj
	 * @return Map
	 */
	/*public static Map obj2Map(Object obj){
		if(obj == null){
			return null;
		}
		Map map=new HashMap();
		Method[] methods=obj.getClass().getMethods();
		for(Method method:methods){
			String field="";	
			String methodName=method.getName();
			if(methodName.contains("get")&&!methodName.contains("getClass")&&method.getParameterCount()==0){
				
				field=methodName.substring(3).substring(0,1)+methodName.substring(3).substring(1);
				try {
					Object value=method.invoke(obj, null);
					map.put(field, value);
				}catch(Exception e){
					log.error(e);
				}
			}
		}
		
		return map;	
	}
	*/
	/**
	 * 判断对象是否为空
	 * @param o
	 * @return
	 */
	public static boolean isNullOrEmpty(Object o){
		boolean flag = false;
		if(o == null){
			flag = true; 
		}
		
		if(o instanceof String){
			if("".equals(o))
				flag = true;
		}
		
		if(o instanceof Collection){
			if( ((Collection) o).size() <= 0 ){
				flag = true;
			}
		}
		
		
		
		
		return flag;
	}
	
	/**
	 * json字符串转换为Map
	 * @param obj
	 * @return
	 * @throws PeException
	 */
	public static Map jsonStr2Map(String jsonStr){
		 ObjectMapper mapper = new ObjectMapper();  
		  Map map = null;
	        try {  
	            // 需要注意的是这里的Map实际为一个LikedHashMap，即链式哈希表，可以按照读入顺序遍历  
	            map = mapper.readValue(jsonStr, Map.class);  
	        } catch (JsonParseException e) {  
	            log.error(e);;  
	        } catch (JsonMappingException e) {  
	            log.error(e);;  
	        } catch (IOException e) {  
	            log.error(e);;  
	        }  
		
		return map;
	}
	
	public static String map2JsonStr(Map map){
		String jsonStr = null;
		ObjectMapper mapper = new ObjectMapper();
		OutputStream out = new java.io.ByteArrayOutputStream();
	    try {
			mapper.writeValue(out, map);
			jsonStr = out.toString();
			out.flush();
		    out.close();
		} catch (JsonGenerationException e) {
			log.error(e);
		} catch (JsonMappingException e) {
			log.error(e);;
		} catch (IOException e) {
			log.error(e);;
		}
	       
		
		return jsonStr;
	}
	
	
	public static void main(String[] args) throws UnsupportedEncodingException {
		Pattern p = Pattern.compile("^[123]$");
		String str = "4";
		Matcher m  = p.matcher(str);
		System.out.println(m.matches());
		
		List list = new ArrayList();
		list.add("22");
		list.add("bb");
		list.add("cc");
		list.forEach(o -> {System.out.println(o);});
		list.stream()
		.filter(b -> "22".equals(b))
		.forEach(o -> {System.out.println(o);});
		
	}
}
